<template>
  <div class="container">
    <p class="title">短信登陆</p>
    <el-form
      :model="smsForm"
      status-icon
      :rules="rules"
      ref="smsForm"
      label-width="100px"
      class="demo-smsForm"
    >
      <el-form-item label="手机号" prop="phonenumber">
        <el-input
          type="password"
          v-model="smsForm.phonenumber"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item label="验证码" prop="verification">
        <el-input v-model.number="smsForm.verification"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('smsForm')">
          提交
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("年龄不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("请输入数字值"));
        } else {
          if (value < 18) {
            callback(new Error("必须年满18岁"));
          } else {
            callback();
          }
        }
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.smsForm.checkPass !== "") {
          this.$refs.smsForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.smsForm.phonenumber) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      smsForm: {
        phonenumber: "",
        verification: "",
      },
      rules: {
        phonenumber: [{ validator: validatePass, trigger: "blur" }],
        verification: [{ validator: checkAge, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less">
.container {
  right: 100px;
  top: 150px;
  color: white;
  width: 380px;
  height: 420px;
  position: absolute;
  background: rgba(255, 255, 255, 0.4);
}
.title {
  text-align: center;
  font-size: 32px;
}
.el-input {
  .el-input__inner {
    width: 240px;
  }
}
.el-form-item__label {
  color: white;
}
.el-button--primary {
  width: 240px;
  background: rgba(245, 178, 215, 0.6);
  border: 0;
}
.base {
  text-decoration: underline;
  font-size: 20px;
  text-align: center;
}
</style>
